//打印欢迎界面
const { promisify } = require('util')

//将应为通过字符画在命令行界面
const figlet = promisify(require('figlet'))

//清理屏幕
const clear = require('clear')

//界面颜色
const chalk = require('chalk')

const log = (content) => console.log(chalk.green(content))

const { clone } = require('./download')
const open = require('open')

const spawns = async (...args) => {
  const { spawn } = require('child_process')
  return new Promise((resolve, reject) => {
    const p = spawn(...args)
    //输出流，子进程，合并到主进程
    p.stdout.pipe(process.stdout)
    p.stderr.pipe(process.stderr)
    p.on('close', () => {
      resolve()
    })
  })
}
module.exports = async (name) => {
  //打印欢迎界面

  //清理屏幕
  clear()

  const data = await figlet('KKB ^_^ YYDS')
  log(data)
  await clone('github:su37josephxia/vue-template', name)
  // 项目模版
  log('成功')
  // await spawns('npm', ['run', 'serve'], { cwd: `./${name}` })
  open('http://localhost:8080')
  await spawns('npm', ['run', 'serve'], { cwd: `./${name}` })
}
